package model

import (
	"gorm.io/gorm"
	"regi-sys/userrpc/userrpc"
)

type User struct {
	gorm.Model
	Account  string `gorm:"type:varchar(20);not null;comment:账号"`
	Password string `gorm:"type:char(32);not null;comment:密码"`
}

func (u *User) UserDetailByAccount(db *gorm.DB, account string) error {
	return db.Where("account = ?", account).First(u).Error
}

func (u *User) UserAdd(db *gorm.DB) error {
	return db.Debug().Create(u).Error
}

func (u *User) UserDelById(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Delete(u).Error
}
func UserLisat(db *gorm.DB, in *userrpc.UserListRequest) []*userrpc.UserList {
	var list []*userrpc.UserList
	db.Model(&User{}).Select(
		"users.id user_id",
		"account",
		"password",
	).Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list)
	return list
}
